// 本地自己的后端
// const baseUrl = 'http://localhost:8080';
const baseUrl = 'http://10.34.80.52:8080';
// const baseUrl = 'http://4c9f8c3d.r24.cpolar.top'
// AI服务接口
// const aiBaseUrl = 'http://localhost:5000'
const aiBaseUrl = 'http://10.34.80.52:5000'
// const aiBaseUrl = 'http://4c9f8c3d.r24.cpolar.top'

/**
 * post 请求
 * @param {*} url 
 * @param {*} data 
 */
function post(url, data,others,postMethod) {
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${baseUrl}${url}`;
  const content_type = others || 'application/json';
  const method = postMethod || 'POST'
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);
  console.log(method)

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: method,
      header: {
       'content-type': content_type,
       'Authorization':token
     },
      data: data,
      success: function(res) {
        console.log(res);
        if (res.data.code == 200) {
          console.log('post请求成功');
          resolve(res);
        } else {
          console.log('post请求发送成功，结果出错');
          resolve(res);
        }
      },
      fail: function(err) {
        console.log('post请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    });
  })
}

function get(url,data,others){
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${baseUrl}${url}`;
  const content_type = others || 'application/json';
  console.log(data);
  console.log(lastUrl);
//   console.log(content_type);

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: 'GET',
      header: {
          'content-type': content_type,
          'Authorization':token
        },
      data: data,
      success: function(res){//请求成功
        // console.log(res);
        if(res.data.code == 200){
        //   console.log('GET请求成功');
          resolve(res);
        }
        else{
          // console.log(res);
          // console.log(res.data)
          console.log('GET请求发送成功，结果出错');
          resolve(res);
        }
      },
      fail: function(err){//请求失败
        console.log('GET请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        })
        reject(err);
      }
    })
  })
}

/**
 * 语音 请求
 * 返回识别后得到的文本
 * @param {*} url 
 * @param {*} data 
 */
function asr(filePath) {
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${aiBaseUrl}/recognize`;
  console.log(lastUrl);

  return new Promise((resolve, reject) => {
    wx.uploadFile({
      filePath: filePath,
      name: 'audio',
      url: lastUrl,
      // formData: {},
      header: { 
        'content-type': 'multipart/form-data' ,
        'Authorization':token
      },
      success: function (res) {
        // 数据存放在res.data.data中
        // res.data 可能是字符串所以需要进行json解析
        const data = JSON.parse(res.data)
        if(data.code == 200 && data.data){
          console.log('语音识别成功')
          // 识别成功返回识别的文本
          resolve(data.data)
        }
      },
      fail:function(err){
        console.log('上传失败，详细错误信息：',err)
        wx.showToast({
          title: '网络连接出现问题，请检查网络设置后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err)
      }
    });
  })
}

/**
 * 专升本提取 请求
 * 发送： “txt”：文本
 * 收到：请求结果，数据在res.data中
 * @param {*} data 
 */
function zsbExtract(data) {
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${aiBaseUrl}/zhuanshengben/extract`;
  const content_type = 'application/json';
  const method = 'POST'
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);
  console.log(method)

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: method,
      header: {
       'content-type': content_type,
       'Authorization':token
     },
      data: data,
      success: function(res) {
        console.log(res);
        console.log('专升本提取请求成功');
        resolve(res);
      },
      fail: function(err) {
        console.log('专升本提取请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    });
  })
}

/**
 * 成考提取 请求
 * 发送： “txt”：文本
 * 收到：请求结果，数据在res.data中
 * @param {*} data 
 */
function ckExtract(data) {
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${aiBaseUrl}/chengkao/extract`;
  const content_type = 'application/json';
  const method = 'POST'
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);
  console.log(method)

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: method,
      header: {
       'content-type': content_type,
       'Authorization':token
     },
      data: data,
      success: function(res) {
        console.log(res);
        console.log('成考提取请求成功');
        resolve(res);
      },
      fail: function(err) {
        console.log('成考提取请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    });
  })
}

/**
 * 自考提取 请求
 * 发送： “txt”：文本
 * 收到：请求结果，数据在res.data中
 * @param {*} data 
 */
function zikaoExtract(data) {
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${aiBaseUrl}/zikao/extract`;
  const content_type = 'application/json';
  const method = 'POST'
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);
  console.log(method)

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: method,
      header: {
       'content-type': content_type,
       'Authorization':token
     },
      data: data,
      success: function(res) {
        console.log(res);
        console.log('自考提取请求成功');
        resolve(res);
      },
      fail: function(err) {
        console.log('自考提取请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    });
  })
}

/**
 * 高考提取 请求
 * 发送： “txt”：文本
 * 收到：请求结果，数据在res.data中
 * @param {*} data 
 */
function gkExtract(data) {
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${aiBaseUrl}/gaokao/extract`;
  const content_type = 'application/json';
  const method = 'POST'
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);
  console.log(method)

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: method,
      header: {
       'content-type': content_type,
       'Authorization':token
     },
      data: data,
      success: function(res) {
        console.log(res);
        console.log('高考提取请求成功');
        resolve(res);
      },
      fail: function(err) {
        console.log('高考提取请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    });
  })
}

/**
 * 中考提取 请求
 * 发送： “txt”：文本
 * 收到：请求结果，数据在res.data中
 * @param {*} data 
 */
function zkExtract(data) {
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${aiBaseUrl}/zhongkao/extract`;
  const content_type = 'application/json';
  const method = 'POST'
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);
  console.log(method)

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: method,
      header: {
       'content-type': content_type,
       'Authorization':token
     },
      data: data,
      success: function(res) {
        console.log(res);
        console.log('中考提取请求成功');
        resolve(res);
      },
      fail: function(err) {
        console.log('中考提取请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    });
  })
}


// 导出函数，供其他文件调用
module.exports = {
  post: post,
  get: get,
  asr: asr,
  zsbExtract: zsbExtract,
  ckExtract: ckExtract,
  zikaoExtract: zikaoExtract,
  gkExtract: gkExtract,
  zkExtract: zkExtract
}